<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/controller/ctlAbstract.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUser.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busAddress.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busLocation.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busPrivacyEmail.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/user.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/couple_invitation.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/generalFunctions.php';

class ProfileController extends AbstractController {
	private $_userBus;
	private $_addressBus;
	private $_locationBus;
	private $_privacyEmailBus;
	
	public function main(array $getVars) {
		global $ubi;
		
		$this->_userBus = new UserBus();
		$this->_addressBus = new AddressBus();
		$this->_locationBus = new LocationBus();
		$this->_privacyEmailBus = new PrivacyEmailBus();
		
		$userID = $_SESSION['userid'];
		if ($userID) {
			$user = $this->_userBus->getUser($userID);
			
			if ($getVars['type'] == 'personal') {
				$user->setLastName($_POST['txtLastName']);
				$user->setFirstName($_POST['txtFirstName']);
				$user->setGender($_POST['selGender']);
				$user->setOccupationID($_POST['selOccupation']);
				$user->setDateOfBirth(GeneralFunctions::getDate($_POST['txtDOB']));
				
				$this->_userBus->updateUser($user);
			} else if ($getVars['type'] == 'contact') {
				$user->setTelephone($_POST['txtTelephone']);
				$user->setCellPhone($_POST['txtCellphone']);
				
				$this->_userBus->updateUser($user);
			} else if ($getVars['type'] == 'email') {
				$user->setEmail($_POST['txtEmail']);
				
				$this->_userBus->updateUser($user);
			} else if ($getVars['type'] == 'address') {
				$address = $this->_addressBus->getAddress($user->getAddressID());
				
				$address->setDistrict($_POST['txtDistrict']);
				$address->setNumber($_POST['txtNumber']);
				$address->setStreet($_POST['txtStreet']);
				$address->setWard($_POST['txtWard']);
				$address->setCity($this->_locationBus->getLocationFromID($_POST['selCity']));
				
				$this->_addressBus->updateAddress($address);
			} else if ($getVars['type'] == 'mariageStatus') {
				$user->setCoupleNickName($_POST['txtCoupleNickName']);
				$user->setCoupleUserName($_POST['txtCoupleUserName']);
				$user->setMariageStatus($_POST['txtMariageStatus']);
				
				$coupleInvitation = new CoupleInvitaionModel();
				$coupleInvitation->setMessage($_POST['txtMessage']);
				$coupleInvitation->setUserID($user->getUserID());
				$coupleInvitation->setDeleteCode(GeneralFunctions::createRandomCode());

				$this->_userBus->updateMariageStatus($user, $coupleInvitation);
			} else if ($getVars['type'] == 'privacy') {
				if (isset($_POST['chkCommnetOnMyPost']) && $_POST['chkCommnetOnMyPost'] == 'commentOnMyPost') {
					$this->_privacyEmailBus->removePrivacyEmail($userID, $ubi['EMAIL']['COMMENT_ON_MY_POST']);
				} else if (!in_array($ubi['EMAIL']['COMMENT_ON_MY_POST'], $user->getEmailPrivacies())){
					$this->_privacyEmailBus->addPrivacyEmail($userID, $ubi['EMAIL']['COMMENT_ON_MY_POST']);
				}
				
				if (isset($_POST['chkWriteOnMyWall']) && $_POST['chkWriteOnMyWall'] == 'writeOnMyWall') {
					$this->_privacyEmailBus->removePrivacyEmail($userID, $ubi['EMAIL']['WRITE_ON_MY_WALL']);
				} else if (!in_array($ubi['EMAIL']['WRITE_ON_MY_WALL'], $user->getEmailPrivacies())){
					$this->_privacyEmailBus->addPrivacyEmail($userID, $ubi['EMAIL']['WRITE_ON_MY_WALL']);	
				}
				
				if (isset($_POST['chkPlusOnMyComment']) && $_POST['chkPlusOnMyComment'] == 'plusOnMyComment') {
					$this->_privacyEmailBus->removePrivacyEmail($userID, $ubi['EMAIL']['PLUS_MY_COMMENT']);	
				} else if (!in_array($ubi['EMAIL']['PLUS_MY_COMMENT'], $user->getEmailPrivacies())){
					$this->_privacyEmailBus->addPrivacyEmail($userID, $ubi['EMAIL']['PLUS_MY_COMMENT']);
					
				}
				
				if (isset($_POST['chkMinusOnMyComment']) && $_POST['chkMinusOnMyComment'] == 'minusOnMyComment') {
					$this->_privacyEmailBus->removePrivacyEmail($userID, $ubi['EMAIL']['MINUS_MY_COMMENT']);
				} else if (!in_array($ubi['EMAIL']['MINUS_MY_COMMENT'], $user->getEmailPrivacies())){
					$this->_privacyEmailBus->addPrivacyEmail($userID, $ubi['EMAIL']['MINUS_MY_COMMENT']);	
					
				}
				
				if (isset($_POST['chkPlusOnMyPost']) && $_POST['chkPlusOnMyPost'] == 'plusOnMyPost') {
					$this->_privacyEmailBus->removePrivacyEmail($userID, $ubi['EMAIL']['PLUS_MY_POST']);
				} else if (!in_array($ubi['EMAIL']['PLUS_MY_POST'], $user->getEmailPrivacies())){
					$this->_privacyEmailBus->addPrivacyEmail($userID, $ubi['EMAIL']['PLUS_MY_POST']);	
				}
				
				if (isset($_POST['chkMinusOnMyPost']) && $_POST['chkMinusOnMyPost'] == 'minusOnMyPost') {
					$this->_privacyEmailBus->removePrivacyEmail($userID, $ubi['EMAIL']['MINUS_MY_POST']);	
				} else if (!in_array($ubi['EMAIL']['MINUS_MY_POST'], $user->getEmailPrivacies())){
					$this->_privacyEmailBus->addPrivacyEmail($userID, $ubi['EMAIL']['MINUS_MY_POST']);
				}
				
				if (isset($_POST['chkFavOnMyPost']) && $_POST['chkFavOnMyPost'] == 'favOnMyPost') {
					$this->_privacyEmailBus->removePrivacyEmail($userID, $ubi['EMAIL']['FAVOURITE_MY_POST']);
				} else if (!in_array($ubi['EMAIL']['FAVOURITE_MY_POST'], $user->getEmailPrivacies())){
					$this->_privacyEmailBus->addPrivacyEmail($userID, $ubi['EMAIL']['FAVOURITE_MY_POST']);					
				}
				
				if (isset($_POST['chkRemoveFavOnMyPost']) && $_POST['chkRemoveFavOnMyPost'] == 'unFavOnMyPost') {
					$this->_privacyEmailBus->removePrivacyEmail($userID, $ubi['EMAIL']['UNFAVOURITE_MY_POST']);	
				} else if (!in_array($ubi['EMAIL']['UNFAVOURITE_MY_POST'], $user->getEmailPrivacies())){
					$this->_privacyEmailBus->addPrivacyEmail($userID, $ubi['EMAIL']['UNFAVOURITE_MY_POST']);
				}
			}
			header("Location: index.php?view&type=profile");
		}
	}
}
?>